#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

#define N 10
void RunChild()
{
    int cnt = 5;
    while(cnt--)
    {
        printf("child, pid: %d, ppid: %d\n", getpid(), getppid());
        sleep(1);
    }
}
int main()
{
    for (int i = 0; i < N; ++i)
    {
        pid_t id = fork();
        if (id == 0)
        {
            RunChild();
            exit(i);
        }
        printf("create child process: %d success\n", id); // 能运行到这里的只有父进程
    }

    //sleep(10);

    // 等待
    for (int i = 0; i < N; ++i)
    {
        int status = 0;
        pid_t id = waitpid(-1, &status, 0);
        if (id > 0)
        {
            printf("wait: %d success, exit code: %d\n", id, WEXITSTATUS(status));
        }
    }

    //sleep(5);

    return 0;
}


